package com.tools.bitbucket.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Slf4j
public class MailUtils {
    private static JavaMailSenderImpl mailSender = SpringUtils.getBean(JavaMailSenderImpl.class);

    public static void sendMail(InternetAddress fromMail, List<InternetAddress> toUserMailList, String subject, String content) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = null;
        try {
            messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            messageHelper.setFrom(fromMail);
            messageHelper.setTo(toUserMailList.toArray(new InternetAddress[]{}));
            messageHelper.setSubject(subject);
            messageHelper.setText(content, true);
            mailSender.send(mimeMessage);
            toUserMailList.stream().forEach(toUserMail -> {
                log.info("===☆☆☆☆☆=== 发送至 {}({})的邮件[{}]发送成功！===☆☆☆☆☆===", toUserMail.getPersonal(), toUserMail.getAddress(), subject);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
